package com.hzncc.scaffold.controller.Front.InstituteCenter;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.CvListBo;
import com.hzncc.scaffold.entity.Bo.InstituteRecordListBo;
import com.hzncc.scaffold.entity.Bo.OnePlusXExamListBo;
import com.hzncc.scaffold.entity.Other.*;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.*;
import com.hzncc.scaffold.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.lang.annotation.ElementType;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-15 14:21
 * @Description: null
 */
@RequestMapping("/instituteCenter")
@Api(tags = "前台接口-机构用户个人中心")
@RestController
public class InstituteCenterController {

    @Autowired
    UserService userService;

    @Autowired
    ApplianceapplyService applianceapplyService;

    @Autowired
    SiteapplyService siteapplyService;

    @Autowired
    JobMapper jobMapper;

    @Autowired
    CvpostService cvpostService;

    @Autowired
    CvpostMapper cvpostMapper;

    @Autowired
    JobService jobService;

    @Autowired
    OneplusxexamMapper oneplusxexamMapper;

    @Autowired
    ExamineeapplyService examineeapplyService;

    @Autowired
    OneplusxexamService oneplusxexamService;

    @Autowired
    OneplusxrecordMapper oneplusxrecordMapper;

    @Autowired
    UserMapper userMapper;


    @GetMapping("/info")
    @ApiOperation("机构用户基本信息")
    public ResultT info() {

        if (!StpUtil.isLogin()) {
            return ResultT.fail("用户未登录");
        }
        User user = userService.getById(StpUtil.getLoginIdAsInt());
        InsituteUserVo insituteUserVo = BeanUtil.copyProperties(user, InsituteUserVo.class);


        LambdaQueryWrapper<Applianceapply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Applianceapply::getInstituteId, StpUtil.getLoginIdAsInt());
        lqw.eq(Applianceapply::getStatus, 1);

        if (CollUtil.isNotEmpty(applianceapplyService.list(lqw))) {
            insituteUserVo.setIsAlliance(1);
        }

        LambdaQueryWrapper<Siteapply> lqww = new LambdaQueryWrapper<>();
        lqww.eq(Siteapply::getInstituteId, StpUtil.getLoginIdAsInt());

        List<Siteapply> siteapplyList = siteapplyService.list(lqww);

        if (CollUtil.isNotEmpty(siteapplyList)) {
            Siteapply siteapply = siteapplyList.get(0);
            if (siteapply.getStatus() == 1) {
                insituteUserVo.setIsTrialSite(1);
            } else if (siteapply.getStatus() == 2) {
                insituteUserVo.setIsTrialSite(3);
            } else if (siteapply.getStatus() == 0) {
                insituteUserVo.setIsTrialSite(2);
            }
        } else {
            insituteUserVo.setIsTrialSite(4);
        }
        return ResultT.success(insituteUserVo);
    }

    @PostMapping("/trialApply")
    @ApiOperation("试点院校申请")
    public ResultT trialApply() {
        if (!StpUtil.isLogin()) {
            return ResultT.fail("用户未登录");
        }
        LambdaQueryWrapper<Siteapply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Siteapply::getInstituteId, StpUtil.getLoginIdAsInt());
        lqw.eq(Siteapply::getStatus, 0);

        if (CollUtil.isNotEmpty(siteapplyService.list(lqw))) {
            return ResultT.fail("正在审核中，请勿多次申请");
        }

        Siteapply siteapply = new Siteapply();

        siteapply.setInstituteId(StpUtil.getLoginIdAsInt());
        siteapply.setCreateTime(DateUtil.now());

        boolean save = siteapplyService.save(siteapply);

        if (save) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }


    @PostMapping("/allianceApply")
    @ApiOperation("申请联盟单位")
    public ResultT allianceApply() {
        if (!StpUtil.isLogin()) {
            return ResultT.fail("当前用户未登录");
        }

        LambdaQueryWrapper<Applianceapply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Applianceapply::getInstituteId, StpUtil.getLoginIdAsInt());
        lqw.eq(Applianceapply::getStatus, 0);

        if (CollUtil.isNotEmpty(applianceapplyService.list(lqw))) {
            return ResultT.fail("正在审核中，请勿多次申请");
        }

        Applianceapply applianceapply = new Applianceapply();
        applianceapply.setInstituteId(StpUtil.getLoginIdAsInt());
        applianceapply.setCreateTime(DateUtil.now());

        boolean save = applianceapplyService.save(applianceapply);
        if (save) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/siteApplyStatus")
    @ApiOperation("站点申报状态")
    public ResultT siteApplyStatus() {
        SiteApplyStatusVo siteApplyStatusVo = new SiteApplyStatusVo();

        LambdaQueryWrapper<Siteapply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Siteapply::getType,1);
        lqw.eq(Siteapply::getInstituteId,StpUtil.getLoginIdAsInt());
        if(CollUtil.isNotEmpty(siteapplyService.list(lqw))){
            if(siteapplyService.list(lqw).get(0).getStatus()==1){
                siteApplyStatusVo.setExamSite(true);
                siteApplyStatusVo.setExamSiteStatus(siteapplyService.list(lqw).get(0).getStatus());
            }else {
                siteApplyStatusVo.setExamSite(false);
                siteApplyStatusVo.setExamSiteStatus(siteapplyService.list(lqw).get(0).getStatus());
            }

        }else {
            siteApplyStatusVo.setExamSite(false);
        }

        LambdaQueryWrapper<Siteapply> lqww = new LambdaQueryWrapper<>();
        lqww.eq(Siteapply::getType,2);
        lqww.eq(Siteapply::getInstituteId,StpUtil.getLoginIdAsInt());
        if(CollUtil.isNotEmpty(siteapplyService.list(lqww))){

            if(siteapplyService.list(lqww).get(0).getStatus()==1){
                siteApplyStatusVo.setManageSite(true);
                siteApplyStatusVo.setManageSiteStatus(siteapplyService.list(lqww).get(0).getStatus());
            }else {
                siteApplyStatusVo.setManageSite(false);
                siteApplyStatusVo.setManageSiteStatus(siteapplyService.list(lqww).get(0).getStatus());
            }

        }else {
            siteApplyStatusVo.setManageSite(false);
        }
        return ResultT.success(siteApplyStatusVo);
    }

    @GetMapping("/jobList")
    @ApiOperation("人才需求列表")
    public ResultT jobList(Integer pageNum, Integer pageSize, Integer status, String keyword) {
        if (!StpUtil.isLogin()) {
            return ResultT.fail("用户未登录");
        }
        QueryWrapper<InstituteCenterJobListVo> qw = new QueryWrapper<>();
        qw.eq("job.belongsTo", StpUtil.getLoginIdAsInt());
        if (status != null) {
            qw.eq("job.status", status);
        }
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("job.title", keyword);
        }
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }

        List<InstituteCenterJobListVo> instituteCenterJobListVos = jobMapper.instituteJobList(qw);


        for (InstituteCenterJobListVo instituteCenterJobListVo : instituteCenterJobListVos) {
            LambdaQueryWrapper<Cvpost> lqww = new LambdaQueryWrapper<>();
            lqww.eq(Cvpost::getJobId, instituteCenterJobListVo.getId());

            List<Integer> cvpostIdList = cvpostService.list(lqww).stream().map(cvpost -> cvpost.getId()).collect(Collectors.toList());
            instituteCenterJobListVo.setCvposts(cvpostIdList);
            instituteCenterJobListVo.setPostCount(cvpostIdList.size());
        }

        return ResultT.success(new PageInfo<>(instituteCenterJobListVos));
    }

    @GetMapping("/cvList")
    @ApiOperation("查看投递列表")
    public ResultT cvList(Integer pageNum, Integer pageSize, String keyword, Integer jobId) {

        if (!StpUtil.isLogin()) {
            return ResultT.fail("用户未登录");
        }

        if (jobId == null) {
            return ResultT.fail("数据错误");
        }

        QueryWrapper<Cvpost> qw = new QueryWrapper<>();
        qw.eq("cvpost.jobId", jobId);
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("cvpost.title", keyword);
        }

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<CvListBo> cvListBos = cvpostMapper.cvList(qw);
        ArrayList<CvListVo> cvListVos = new ArrayList<>();

        for (CvListBo cvListBo : cvListBos) {
            CvListVo cvListVo = BeanUtil.toBean(cvListBo, CvListVo.class);

            CvListUserVo cvListUserVo = BeanUtil.toBean(userService.getById(StpUtil.getLoginIdAsInt()), CvListUserVo.class);
            cvListVo.setUser(cvListUserVo);

            JobIdAndTitleVo jobIdAndTitleVo = new JobIdAndTitleVo();
            jobIdAndTitleVo.setId(cvListBo.getJobId());
            jobIdAndTitleVo.setTitle(cvListBo.getJobTitle());

            cvListVo.setJob(jobIdAndTitleVo);

            cvListVos.add(cvListVo);
        }

        PageInfo<CvListVo> info = new PageInfo<>();
        info.setList(cvListVos);
        info.setTotal(new PageInfo<>(cvListBos).getTotal());

        return ResultT.success(info);
    }

    @PostMapping("/deleteCv")
    @ApiOperation("删除投递")
    public ResultT deleteCv(@RequestBody CvIdVo cvIdVo) {
        Integer cvId = cvIdVo.getCvId();
        if (cvpostService.getById(cvId) == null) {
            return ResultT.fail("数据错误");
        }

        boolean remove = cvpostService.removeById(cvId);

        if (remove) {
            return ResultT.success("删除成功");
        } else {
            return ResultT.fail("删除失败");
        }
    }

    @PostMapping("/updateJob")
    @ApiOperation("修改人才需求列表")
    public ResultT updateJob(@RequestBody InstituteUpdateJobVo instituteUpdateJobVo) {
        Integer jobId = instituteUpdateJobVo.getJobId();
        if (jobService.getById(jobId) == null) {
            return ResultT.fail("数据错误");
        }

        Job job = jobService.getById(jobId);
        job.setAddress(instituteUpdateJobVo.getAddress());
        job.setContent(instituteUpdateJobVo.getContent());
        job.setEducation(instituteUpdateJobVo.getEducation());
        job.setNeedNum(instituteUpdateJobVo.getNeedNum());
        job.setSalary(instituteUpdateJobVo.getSalary());
        job.setTitle(instituteUpdateJobVo.getTitle());
        job.setType(instituteUpdateJobVo.getType());

        boolean update = jobService.updateById(job);
        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }

    @PostMapping("/deleteJob")
    @ApiOperation("删除人才需求")
    public ResultT deleteJob(@RequestBody JobIdVo jobIdVo) {
        Integer jobId = jobIdVo.getJobId();

        if (jobService.getById(jobId) == null) {
            return ResultT.fail("数据错误");
        }

        boolean remove = jobService.removeById(jobId);

        if (remove) {

            LambdaQueryWrapper<Cvpost> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Cvpost::getJobId, jobId);
            cvpostService.remove(lqw);
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }

    }


    @PostMapping("/addJob")
    @ApiOperation("新增招聘职位")
    public ResultT addJob(@RequestBody InstituteAddJobVo instituteAddJobVo) {

        if (!StpUtil.isLogin()) {
            return ResultT.fail("当前用户未登录");
        }

        Job job = BeanUtil.toBean(instituteAddJobVo, Job.class);
        job.setBelongsTo(StpUtil.getLoginIdAsInt());
        job.setCreateTime(DateUtil.now());
        job.setCreateBy("institute");
        job.setSortTime(DateUtil.now());

        boolean save = jobService.save(job);

        if (save) {
            return ResultT.success("新增成功");
        } else {
            return ResultT.fail("新增失败");
        }
    }

    @GetMapping("/onePlusXExamList")
    @ApiOperation("考试计划申报列表")
    public ResultT onePlusXExamList(Integer pageNum, Integer pageSize, String keyword) {
        if (!StpUtil.isLogin()) {
            return ResultT.fail("当前用户未登录");
        }
        QueryWrapper<OnePlusXExamListBo> qw = new QueryWrapper<>();
        qw.eq("oneplusxexam.creatorId", StpUtil.getLoginIdAsInt());
        qw.like("oneplusxexam.title", keyword);

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<OnePlusXExamListBo> onePlusXExamListBos = oneplusxexamMapper.listBo(qw);
        ArrayList<OnePlusXExamListVo> onePlusXExamListVos = new ArrayList<>();


        for (OnePlusXExamListBo onePlusXExamListBo : onePlusXExamListBos) {
            OnePlusXExamListVo onePlusXExamListVo = BeanUtil.toBean(onePlusXExamListBo, OnePlusXExamListVo.class);

            ProfessionVo professionVo = new ProfessionVo();
            professionVo.setId(onePlusXExamListBo.getProfessionId());
            professionVo.setName(onePlusXExamListBo.getProfessionName());
            onePlusXExamListVo.setProfession(professionVo);

            SkillVo skillVo = new SkillVo();
            skillVo.setId(onePlusXExamListBo.getSkillId());
            skillVo.setName(onePlusXExamListBo.getSkillName());
            onePlusXExamListVo.setSkill(skillVo);

            LambdaQueryWrapper<Examineeapply> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Examineeapply::getFinalCheck, 0);
            lqw.eq(Examineeapply::getExamId, onePlusXExamListBo.getId());
            List<Integer> applyIdList = examineeapplyService.list(lqw).stream().map(examineeapply -> examineeapply.getId()).collect(Collectors.toList());

            onePlusXExamListVo.setExamineeApplies(applyIdList);


            LambdaQueryWrapper<Examineeapply> lqww = new LambdaQueryWrapper<>();
            lqww.eq(Examineeapply::getFinalCheck, 1);
            lqww.eq(Examineeapply::getExamId, onePlusXExamListBo.getId());
            int examineeCount = examineeapplyService.count(lqww);

            onePlusXExamListVo.setExamineeCount(examineeCount);

            onePlusXExamListVos.add(onePlusXExamListVo);
        }

        PageInfo<OnePlusXExamListVo> info = new PageInfo<>();
        info.setList(onePlusXExamListVos);
        info.setTotal(new PageInfo<>(onePlusXExamListBos).getTotal());

        return ResultT.success(info);
    }

    @PostMapping("/addExam")
    @ApiOperation("新增考试计划")
    public ResultT addExam(@RequestBody AddExamVo addExamVo) {

        if (!StpUtil.isLogin()) {
            return ResultT.fail("当前用户未登录");
        }

        Oneplusxexam oneplusxexam = BeanUtil.toBean(addExamVo, Oneplusxexam.class);
        oneplusxexam.setCreateTime(DateUtil.now());
        oneplusxexam.setCreatorId(StpUtil.getLoginIdAsInt());

        boolean save = oneplusxexamService.save(oneplusxexam);

        if (save) {
            return ResultT.success("新增成功");
        } else {
            return ResultT.fail("新增失败");
        }
    }

    @PostMapping("/deleteExam")
    @ApiOperation("删除考试计划")
    public ResultT deleteExam(@RequestBody ExamIdVo examIdVo) {
        String examId = examIdVo.getExamId();

        if (oneplusxexamService.getById(examId) == null) {
            return ResultT.fail("数据错误");
        }

        LambdaQueryWrapper<Examineeapply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Examineeapply::getExamId, examId);
        examineeapplyService.remove(lqw);

        boolean remove = oneplusxexamService.removeById(examId);
        if (remove) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }

    }


    @PostMapping("/updateExam")
    @ApiOperation("修改考试计划")
    public ResultT updateExam(@RequestBody InstituteUpdateExamVo InstituteUpdateExamVo) {

        LambdaQueryWrapper<Oneplusxexam> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Oneplusxexam::getId, InstituteUpdateExamVo.getExamId());
        Oneplusxexam oneplusxexam = oneplusxexamService.getOne(lqw);

        oneplusxexam.setActivityEnd(InstituteUpdateExamVo.getActivityEnd());
        oneplusxexam.setActivityStart(InstituteUpdateExamVo.getActivityStart());
        oneplusxexam.setDevice(InstituteUpdateExamVo.getDevice());
        oneplusxexam.setDeviceType(InstituteUpdateExamVo.getDeviceType());
        oneplusxexam.setEndTime(InstituteUpdateExamVo.getEndTime());
        oneplusxexam.setExamType(InstituteUpdateExamVo.getExamType());
        oneplusxexam.setExamineeAmount(InstituteUpdateExamVo.getExamineeAmount());
        oneplusxexam.setInfo(InstituteUpdateExamVo.getInfo());
        oneplusxexam.setProfessionId(InstituteUpdateExamVo.getProfessionId());
        oneplusxexam.setSkillId(InstituteUpdateExamVo.getSkillId());
        oneplusxexam.setStartTime(InstituteUpdateExamVo.getStartTime());
        oneplusxexam.setTitle(InstituteUpdateExamVo.getTitle());

        boolean update = oneplusxexamService.updateById(oneplusxexam);
        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }


    @PostMapping("/commitExam")
    @ApiOperation("提交考试")
    public ResultT commitExam(@RequestBody ExamIdVo examIdVo) {
        String examId = examIdVo.getExamId();

        if (oneplusxexamService.getById(examId) == null) {
            return ResultT.fail("数据错误");
        }

        Oneplusxexam oneplusxexam = oneplusxexamService.getById(examId);
        oneplusxexam.setStatus(1);

        boolean update = oneplusxexamService.updateById(oneplusxexam);

        if (update) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/registExaminee")
    @ApiOperation("新增考生")
    public ResultT registExaminee(@RequestBody RegistExamineeVo registExamineeVo) {


        AddUserVo addUserVo = new AddUserVo();
        addUserVo.setGender(registExamineeVo.getGender());
        addUserVo.setIdCardNum(registExamineeVo.getIdCardNum());
        addUserVo.setInstituteName(registExamineeVo.getInstituteName());
        addUserVo.setMajor(registExamineeVo.getMajor());
        addUserVo.setRealname(registExamineeVo.getRealname());
        addUserVo.setTelephone(registExamineeVo.getTelephone());
        addUserVo.setType(1);

        ResultT resultT = userService.addUser(addUserVo);

        if (resultT.getCode() == 200) {
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getIdCardNum, registExamineeVo.getIdCardNum());
            User user = userService.getOne(lqw);


            ExistAndUserIdVo existAndUserIdVo = new ExistAndUserIdVo();
            existAndUserIdVo.setExist(0);
            existAndUserIdVo.setUserId(user.getId());

            return ResultT.success(existAndUserIdVo);
        } else {
            return ResultT.fail("操作失败");
        }
    }


    @PostMapping("/addExaminee")
    @ApiOperation("添加考生")
    public ResultT addExaminee(@RequestBody AddExamineeVo addExamineeVo) {
        Examineeapply examineeapply = new Examineeapply();
        examineeapply.setUserId(addExamineeVo.getUserId());
        examineeapply.setExamId(addExamineeVo.getExamId());
        examineeapply.setCreateTime(DateUtil.now());

        boolean save = examineeapplyService.save(examineeapply);
        if (save) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/commitExaminee")
    @ApiOperation("上报考生")
    @Transactional
    public ResultT commitExaminee(@RequestBody ApplyIdArrVo applyIdArrVo) {
        List<Integer> applyIdArr = applyIdArrVo.getApplyIdArr();

        for (Integer applyId : applyIdArr) {
            if (examineeapplyService.getById(applyId) != null) {
                Examineeapply examineeapply = examineeapplyService.getById(applyId);

                examineeapply.setIsCommitted(true);
                examineeapplyService.updateById(examineeapply);
            }
        }
        return ResultT.success("上报成功");
    }

    @GetMapping("/examineeList")
    @ApiOperation("考生列表")
    public ResultT examineeList(Integer pageNum, Integer pageSize, Integer status, String examId) {

        if (status != 0 && status != 1) {
            return ResultT.fail("数据错误");
        }

        LambdaQueryWrapper<Examineeapply> lqw = new LambdaQueryWrapper<>();

        if (status == 0) {
            lqw.eq(Examineeapply::getFirstCheck, 0);
        }

        if (status == 1) {
            lqw.eq(Examineeapply::getFirstCheck, 1);
        }
        if (StrUtil.isNotEmpty(examId)) {
            lqw.eq(Examineeapply::getExamId, examId);
        }

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Examineeapply> examineeapplyList = examineeapplyService.list(lqw);
        ArrayList<InstituteExamineeListVo> instituteExamineeListVos = new ArrayList<>();

        for (Examineeapply examineeapply : examineeapplyList) {
            InstituteExamineeListVo instituteExamineeListVo = BeanUtil.toBean(examineeapply, InstituteExamineeListVo.class);

            Integer userId = examineeapply.getUserId();
            if (userService.getById(userId) != null) {
                User creator = userService.getById(userId);
                instituteExamineeListVo.setCreator(creator);
            }

            instituteExamineeListVos.add(instituteExamineeListVo);
        }

        PageInfo<InstituteExamineeListVo> info = new PageInfo<>();
        info.setList(instituteExamineeListVos);
        info.setTotal(new PageInfo<>(examineeapplyList).getTotal());

        return ResultT.success(info);
    }


    @PostMapping("/deleteExamineeApply")
    @ApiOperation("删除考生")
    public ResultT deleteExamineeApply(@RequestBody ApplyIdVo applyIdVo) {
        Integer applyId = applyIdVo.getApplyId();

        if (examineeapplyService.getById(applyId) == null) {
            return ResultT.fail("数据错误");
        }
        if (examineeapplyService.getById(applyId).getIsCommitted()) {
            return ResultT.fail("已上报 不能删除！");
        }

        boolean remove = examineeapplyService.removeById(applyId);

        if (remove) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/updateExaminee")
    @ApiOperation("修改考生信息")
    public ResultT updateExaminee(@RequestBody InstituteUpdateExamineeVo instituteUpdateExamineeVo) {
        Integer applyId = instituteUpdateExamineeVo.getApplyId();

        QueryWrapper<User> qw = new QueryWrapper<>();
        List<String> telePhoneList = userMapper.telePhoneList(qw);

        if (CollUtil.contains(telePhoneList, instituteUpdateExamineeVo.getTelephone())) {
            return ResultT.fail("该手机号已被注册");
        }

        List<String> idCardNumList = userMapper.idCardNumList(qw);

        if (CollUtil.contains(idCardNumList, instituteUpdateExamineeVo.getIdCardNum())) {
            return ResultT.fail("该身份证号码已被注册");
        }

        if (examineeapplyService.getById(applyId) == null) {
            return ResultT.fail("数据错误");
        }

        Integer userId = examineeapplyService.getById(applyId).getUserId();

        if (userService.getById(userId) == null) {
            return ResultT.fail("数据错误");
        }

        User user = userService.getById(userId);
        user.setGender(instituteUpdateExamineeVo.getGender());
        user.setIdCardNum(instituteUpdateExamineeVo.getIdCardNum());
        user.setInstituteName(instituteUpdateExamineeVo.getInstituteName());
        user.setMajor(instituteUpdateExamineeVo.getMajor());
        user.setRealname(instituteUpdateExamineeVo.getRealname());
        user.setTelephone(instituteUpdateExamineeVo.getTelephone());

        boolean update = userService.updateById(user);
        if (update) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @PostMapping("/updateExamineeApplyStatus")
    @ApiOperation("修改上报考生申请")
    public ResultT updateExamineeApplyStatus(@RequestBody ApplyIdAndFirstCheckVo applyIdAndFirstCheckVo) {
        Integer applyId = applyIdAndFirstCheckVo.getApplyId();
        Integer firstCheck = applyIdAndFirstCheckVo.getFirstCheck();

        if (examineeapplyService.getById(applyId) == null) {
            return ResultT.fail("数据错误");
        }

        Examineeapply examineeapply = examineeapplyService.getById(applyId);
        examineeapply.setFirstCheck(firstCheck);
        boolean update = examineeapplyService.updateById(examineeapply);

        if (update) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }
    }

    @GetMapping("/recordList")
    @ApiOperation("成绩与证书列表")
    public ResultT recordList(Integer pageNum, Integer pageSize, Integer professionId, String keyword) {

        QueryWrapper<InstituteRecordListBo> qw = new QueryWrapper<>();
        if (professionId != null) {
            qw.eq("oneplusxexam.professionId", professionId);
        }
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("owner.realname", keyword).or().like("owner.idCardNum", keyword).or().like("owner.telephone", keyword).or().like("owner.instituteName", keyword);
        }

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<InstituteRecordListBo> instituteRecordListBos = oneplusxrecordMapper.InstituteRecordList(qw);
        ArrayList<InstituteRecordListVo> instituteRecordListVos = new ArrayList<>();

        for (InstituteRecordListBo instituteRecordListBo : instituteRecordListBos) {
            InstituteRecordListVo instituteRecordListVo = BeanUtil.toBean(instituteRecordListBo, InstituteRecordListVo.class);

            OwnerVo ownerVo = new OwnerVo();
            ownerVo.setId(instituteRecordListBo.getOwnerId());
            ownerVo.setGender(instituteRecordListBo.getOwnerGender());
            ownerVo.setIdCardNum(instituteRecordListBo.getOwnerIdCardNum());
            ownerVo.setInstituteName(instituteRecordListBo.getOwnerInstituteName());
            ownerVo.setRealname(instituteRecordListBo.getOwnerRealname());
            ownerVo.setTelephone(instituteRecordListBo.getOwnerTelephone());

            InstituteOnePlusxExamDetailVo onePlusxExamDetailVo = new InstituteOnePlusxExamDetailVo();
            onePlusxExamDetailVo.setId(instituteRecordListBo.getOnePlusXExamId());
            onePlusxExamDetailVo.setExamType(instituteRecordListBo.getOnePlusXExamExamType());
            onePlusxExamDetailVo.setTitle(instituteRecordListBo.getOnePlusXExamTitle());

            ProfessionVo professionVo = new ProfessionVo();
            professionVo.setId(instituteRecordListBo.getOnePlusXExamProfessionId());
            professionVo.setName(instituteRecordListBo.getOnePlusXExamProfessionName());

            onePlusxExamDetailVo.setProfession(professionVo);

            SkillVo skillVo = new SkillVo();
            skillVo.setId(instituteRecordListBo.getOnePlusXExamSkillId());
            skillVo.setName(instituteRecordListBo.getOnePlusXExamSkillName());

            onePlusxExamDetailVo.setSkill(skillVo);

            instituteRecordListVo.setOwner(ownerVo);
            instituteRecordListVo.setOnePlusXExam(onePlusxExamDetailVo);


            instituteRecordListVos.add(instituteRecordListVo);
        }

        PageInfo<InstituteRecordListVo> info = new PageInfo<>();
        info.setList(instituteRecordListVos);
        info.setTotal(new PageInfo<>(instituteRecordListBos).getTotal());

        return ResultT.success(info);
    }

    @PostMapping("/siteApplyMultiFiles")
    @ApiOperation("站点申报申请")
    public ResultT siteApplyMultiFiles(@RequestBody SiteApplyMultiFilesVo siteApplyMultiFilesVo) {
        if (siteApplyMultiFilesVo.getType() != 1 && siteApplyMultiFilesVo.getType() != 2) {
            return ResultT.fail("数据错误");
        }

            Siteapply siteapply = new Siteapply();
            siteapply.setCreateTime(DateUtil.now());
            siteapply.setInstituteId(StpUtil.getLoginIdAsInt());
            siteapply.setContent(siteApplyMultiFilesVo.getContent());
            siteapply.setType(siteApplyMultiFilesVo.getType());

            boolean save = siteapplyService.save(siteapply);
            if (save) {
                return ResultT.success("操作成功");
            } else {
                return ResultT.fail("操作失败");
            }
    }
}
